package com.r_z.mapper;

import com.r_z.entity.FiscalExpenditure;
import com.r_z.entity.Purchase;
import com.r_z.entity.PurchaseDetails;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author 周楠櫻木
 * @version 1.0
 * @date 2023/6/8
 */
@Mapper
public interface PurchaseMapper {
    /**
     * 查询最大ID
     */
    @Select("SELECT MAX(purchaseId) FROM purchase")
    String purchaseMaxId();

    /**
     * 查询最大ID
     */
    @Select("SELECT MAX(fExpenditureId) FROM fiscalexpenditure")
    String fExpenditureIdMax();

    /**
     * 查询采购列表的方法(查询总页数)
     * @return 采购列表实体集
     */
    @Select("select * from purchase where purchaseId like '%${purchaseId}%'")
    List<Purchase> selectPurchaseAll(@Param("purchaseId") String goodName);

    /**
     * 查询采购列表的方法(分页以及模糊查询)
     * @return 采购列表实体集
     */
    @Select("select * from purchase where purchaseId like '%${purchaseId}%' limit ${page},${limit}")
    List<Purchase> selectPurchase(@Param("purchaseId") String goodName,@Param("page") int page,@Param("limit") int limit);

    /**
     * 查询采购列表关联的用户名称
     */
    @Select("select u.userName from purchase p join users u on p.userId=u.userId")
    List<Purchase> selectUserName();

    /**
     * 采购列表的增加方法
     * @param purchase 采购列表实体类
     * @return boolean
     */
    @Insert("insert into purchase values(#{purchaseId},#{purchaseNum},#{total},#{auditState}," +
            "#{paymentState},#{storageinState},#{nowTime},#{userId})")
    boolean insertPurchase(Purchase purchase);

    /**
     * 采购详情列表的增加方法
     * @param purchaseDetails 采购详情列表实体类
     */
    @Insert("insert into purchaseDetails(price,quantity,totalDetails,goodId,purchaseId) values(#{price},#{quantity},#{totalDetails},#{goodId},#{purchaseId})")
    void insertPurchaseDetails(PurchaseDetails purchaseDetails);

    /**
     * 根据采购单号查找采购详情表
     * @param purchaseId 采购单号
     * @return 采购详情实体集
     */
    @Select("select * from purchaseDetails where purchaseId = #{purchaseId}")
    List<PurchaseDetails> selectPurchaseDetails(String purchaseId);

    /**
     * 查询采购商品表的方法
     * @return 采购商品的实体集合
     */
    @Select("select goodName from goods where goodId = ${goodId} ")
    String selectGoods(@Param("goodId") int goodId);

    /**
     * 删除所有采购计划数据的方法
     */
    @Delete("delete from purchasePlan")
    boolean dropPurchasePlanAll();

    /**
     * 采购报表的查询方法 (查询月份)
     */
    @Select("select g.goodName,p.* from goods g join purchaseDetails p on g.goodId = p.goodId  where p.purchaseId like '%${purchaseId}%'")
    List<PurchaseDetails> selectStatement(@Param("purchaseId") String purchaseId);

    /**
     * 财务支出表的增加方法
     */
    @Insert("insert into fiscalExpenditure values(#{fExpenditureId},#{purchaseId},#{money},#{data_time},#{audit_state},#{type},#{describes})")
    void insertFiscalExpenditure(FiscalExpenditure fiscalExpenditure);

    /**
     * 采购列表的修改方法
     */
    @Update("UPDATE purchase SET purchaseNum=${purchaseNum},total=${total} WHERE purchaseId='${purchaseId}'")
    void updatePurchase(@Param("purchaseNum") int purchaseNum,@Param("purchaseId") String purchaseId,@Param("total") double total);
}

